package frans

import "sync"

//协程任务组件

func init() {

	chList := getTaskList()

	go func() {
		for t := range chList {
			t.Exec()
		}
	}()
}

type TaskFunc func(params ...interface{}) // 任务函数类型

var taskList chan *TaskExecutor
var once sync.Once

func getTaskList() chan *TaskExecutor {

	once.Do(func() {
		taskList = make(chan *TaskExecutor)
	})

	return taskList
}

//任务执行者
type TaskExecutor struct {
	f TaskFunc
	p []interface{}
}

//构造函数
func NewTaskExecutor(f TaskFunc, p []interface{}) *TaskExecutor {
	return &TaskExecutor{f: f, p: p}
}

//执行任务
func (this *TaskExecutor) Exec() {
	this.f(this.p...)
}

func Task(f TaskFunc, params ...interface{}) {

	//taskList := getTaskList()
	//taskList <- NewTaskExecutor(f, params)

	getTaskList() <- NewTaskExecutor(f, params)

	return
}
